﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Dss.Methods;

namespace CECB
{
    /// <summary>
    /// Interaction logic for Email_Window.xaml
    /// </summary>
    public partial class Email_Window : Window
    {
        Dss.Methods.Email em;

        public Email_Window()
        {
            InitializeComponent();
            em = new Dss.Methods.Email();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_MemberFirstName_DropDownOpened(object sender, EventArgs e)
        {
            comboBox_MemberFirstName.ItemsSource = em.getAllfNames();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void comboBox_MemberFirstName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            String x = comboBox_MemberFirstName.SelectedValue.ToString();
            //  String memRef = em.getMemberRefByfName("Admin");
            label_MemberReference.Content = x;
        }

        private void button_Generate_Click(object sender, RoutedEventArgs e)
        {

            textBox_Password.Text = em.genarateRandomPassword(20);
        }

        private void comboBox_MemberEmail_DropDownOpened(object sender, EventArgs e)
        {
            if (comboBox_MemberFirstName.SelectedIndex == -1)
            {
                MessageBox.Show("Select Member Name");
            }
            else
            {
                comboBox_MemberEmail.ItemsSource = em.getMemberEmailByRef(label_MemberReference.Content.ToString());
            }
        }

        private void button_SendMail_Click(object sender, RoutedEventArgs e)
        {


            if (comboBox_MemberFirstName.SelectedIndex == -1)
            {
                MessageBox.Show("Select Member Name");
            }
            else if (comboBox_MemberEmail.SelectedIndex == -1)
            {
                MessageBox.Show("Select Member Email");
            }
            else if (textBox_Password.Text.Length == 0)
            {
                MessageBox.Show("Genarate Activation Code");
            }
            else
            {
                String from = "sliititb1@gmail.com";
                String to = comboBox_MemberEmail.SelectedValue.ToString();
                String cc = "";
                String displayName = "CECB";
                String password = "900760922v";
                String Subjet = "Informing Activation Code";
                String Body = "   Activation Code: " + textBox_Password.Text.ToString();

                bool mailSent = em.genarateEmail(from, to, cc, displayName, password, Subjet, Body);

                if (mailSent == true)
                    MessageBox.Show("Email sent Succesfully");
                else
                    MessageBox.Show("Email sending failed");


            }


        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        public void clear()
        {
            comboBox_MemberFirstName.SelectedIndex = -1;
            comboBox_MemberEmail.SelectedIndex = -1;
            textBox_Password.Clear();
            label_MemberReference.Content = "______________";

        }
    }
}
